//
//  TKL_HomeVC.m
//  TKL_EnPro
//
//  Created by 雅恩三河科技 on 2018/12/7.
//  Copyright © 2018年 MrZhang. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "TKL_HomeVC.h"
#import "TKL_HomeViewModel.h"
#import "TKL_BeautyDetailVC.h"
#import "TKL_HomeCityDetailVC.h"
#import "TKL_homeDetailWebVC.h"
#import "TKL_VideoRootVC.h"
#import "TKL_ImageDetailVC.h"
#import "RKL_WedClothVC.h"
#import "TKL_CityPhotoVC.h"
#import "TKL_HomeNewPhotos.h"
#import "TKL_VideoListVC.h"
@interface TKL_HomeVC ()<SDCycleScrollViewDelegate>
@property(nonatomic,strong)TKL_HomeViewModel* viewModel;
@property(nonatomic,strong)NSMutableArray* dataSource;
@end

@implementation TKL_HomeVC
#pragma mark - getter and setter
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource=[NSMutableArray new];

    }
    return _dataSource;
}
-(TKL_HomeViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel=[TKL_HomeViewModel new];
    }
    return _viewModel;
}
#pragma mark - event
#pragma mark - method
-(void)initilization
{
    self.isGroup = YES;
    @WeakSelf;
    
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.1f];
    
    self.navigationItem.title =@"首页";
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.baseTable.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight], 0);
    self.view.backgroundColor = Main_Color_BG;
    
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.baseTable setSeparatorColor:Main_Color_BG];
    [self viewModelBand];
    
    
    [self.dataSource addObject:@[@"image60.jpg",@"image62.jpg",@"image65.jpg",@"image67.jpg",@"image69.jpg"]];
    [self.dataSource addObject:@[@"image71.jpg",@"image73.jpg",@"image75.jpg",@"image78.jpg",@"image77.jpg"]];
    
    
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfp reloadData];
        [selfp.baseTable.mj_header endRefreshing];
    }];
    [selfp reloadData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel* saleOber1=[GCBaseObservationModel new];
    saleOber1.keyPath=@"itemArr";
    saleOber1.observation=self.viewModel;
    saleOber1.handler = ^(NSString *keyPath) {
        [selfp.baseTable reloadData];
    };
    [self registObservation:saleOber1];
    
    
    GCBaseObservationModel* saleOber2=[GCBaseObservationModel new];
    saleOber2.keyPath=@"datas";
    saleOber2.observation=self.viewModel;
    saleOber2.handler = ^(NSString *keyPath) {
        [selfp.baseTable reloadData];;
    };
    [self registObservation:saleOber2];
    
    GCBaseObservationModel* saleOber3=[GCBaseObservationModel new];
    saleOber3.keyPath=@"beautyArr";
    saleOber3.observation=self.viewModel;
    saleOber3.handler = ^(NSString *keyPath) {
        [selfp.baseTable reloadData];
    };
    [self registObservation:saleOber3];
    
    //
    
    
    GCBaseObservationModel* saleOber555=[GCBaseObservationModel new];
    saleOber555.keyPath=@"souceDic";
    saleOber555.observation=self.viewModel;
    saleOber555.handler = ^(NSString *keyPath) {
        if (selfp.viewModel.souceDic.count!=0)
        {

        }
    };
    [self registObservation:saleOber555];
    
}
-(void)reloadListData
{
    NSMutableDictionary* para=[NSMutableDictionary new];
    [para setObject:@"iPhone" forKey:@"device"];
    [para setObject:@"济南" forKey:@"city"];
    [self.viewModel  requestCityData:para];
    [self.viewModel requestBeautyData:para];
    [para setObject:@"app_trip_banner" forKey:@"position"];
    [self.viewModel requestAdData:para];

}
-(void)reloadData
{
    [self reloadListData];
    [self.viewModel requestPass];
    
//    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"newslist"];
//    [bquery whereKey:@"ishome" equalTo:@"1"];
//    //查找GameScore表的数据
//    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
//        for (BmobObject *obj in array) {
//            //打印playerName
//            NSLog(@"obj.playerName = %@", [obj objectForKey:@"name"]);
//            //打印objectId,createdAt,updatedAt
//            NSLog(@"obj.objectId = %@", [obj objectForKey:@"ishome"]);
//            NSLog(@"obj.createdAt = %@", [obj createdAt]);
//            NSLog(@"obj.updatedAt = %@", [obj updatedAt]);
//            
//        }
//        self.dataSource=[[NSMutableArray alloc] initWithArray:array];
//        [self.baseTable reloadData];
//    }];
    
    
    
}
-(void)reloadView
{
    
}
#pragma mark - life

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}
#pragma mark - delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0||section==1)
    {
        return 1;
    }

    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @WeakSelf;
    if (indexPath.section==0)
    {
        static NSString* identify=@"ppPhonecell11";
        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor whiteColor];
        }
        [cell.contentView removeAllSubviews];
        
        SDCycleScrollView* bannerScroll=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WFCGFloatX(375), WFCGFloatY(160)) delegate:self placeholderImage:DefaultImage];
        bannerScroll.backgroundColor=Main_Color_BGLightGray;
        bannerScroll.currentPageDotColor=Main_Color;
        bannerScroll.pageDotColor=[UIColor whiteColor];
        bannerScroll.pageControlBottomOffset=5;
        [cell.contentView addSubview:bannerScroll];
        NSMutableArray* imageArr=[[NSMutableArray alloc] init];
        for (TKL_HomeAdModel* model in self.viewModel.itemArr)
        {
           //[imageArr addObject:model.imageUrl];
        }
        [imageArr addObject:@"http://www.jnwmxn.com/templets/f/style/images/ban-yp.jpg"];
        [imageArr addObject:@"http://www.jnwmxn.com/templets/f/style/images/ban-kp.jpg"];
        [imageArr addObject:@"http://www.jnwmxn.com/upload/image/2017/11/1511930835-9476.jpg"];
        [imageArr addObject:@"http://www.jnwmxn.com/upload/image/2019/04/1556099795-1818.jpg"];
        bannerScroll.imageURLStringsGroup=imageArr;
        return cell;
    }
    
    if (indexPath.section==1)
    {
        static NSString* identify=@"ppPhonecell222";
        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell)
        {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor whiteColor];
        }
        [cell.contentView removeAllSubviews];
        NSArray* arr=@[@"主题样片",@"最新客片",@"全球旅拍",@"专属礼服"];
        float ww=(SCREEN_WIDTH-WFCGFloatX(75))/4;
        
        for (int i=0; i<arr.count; i++)
        {
            UIView* backView=[UIView new];
            backView.width=ww;
            backView.height=ww;
            backView.x=WFCGFloatX(15)+(WFCGFloatX(15)+ww)*(i%4);
            backView.y=WFCGFloatY(15);
            backView.backgroundColor=[UIColor whiteColor];
            [cell.contentView addSubview:backView];
            
            UIImageView* markIV=[UIImageView new];
//            markIV.width=(ww-WFCGFloatX(20));
//            markIV.height=markIV.width;
            markIV.width=36;
            markIV.height=36;
            markIV.x=(ww-markIV.width)/2;
            markIV.y=WFCGFloatY(0);
            [backView addSubview:markIV];
            markIV.image=[UIImage imageNamed:[NSString stringWithFormat:@"home_new_icon_%d",i]];
            
            UILabel* titleLab=[UILabel new];
            titleLab.x=WFCGFloatX(0);
            titleLab.y=ww-WFCGFloatY(20);
            titleLab.width=ww;
            titleLab.height=WFCGFloatY(15);
            titleLab.textColor=HEXCOLOR(@"#666666");
            titleLab.font=SYSTEMFONT(14);
            [backView addSubview:titleLab];
            titleLab.text=arr[i];
            titleLab.textAlignment=NSTextAlignmentCenter;
            
            
            [backView addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                if (i==0) {
                    
                    TKL_VideoListVC* vc=[[TKL_VideoListVC alloc] init];
                    [selfp.navigationController pushViewController:vc animated:YES];
                }
                if (i==1) {
                    TKL_HomeNewPhotos* vc=[[TKL_HomeNewPhotos alloc] init];
                    [selfp.navigationController pushViewController:vc animated:YES];
                }

                if (i==2) {
                    TKL_CityPhotoVC* vc=[[TKL_CityPhotoVC alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                if (i==3) {
                    RKL_WedClothVC* vc=[[RKL_WedClothVC alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                
            
            }];
            
        }
        
        
        
//        for (int i=0; i<self.viewModel.datas.count; i++)
//        {
//            TKL_HomeCityModel* model=self.viewModel.datas[i];
//            UIImageView* lineIV=[UIImageView new];
//            lineIV.width=WFCGFloatX(105);
//            lineIV.height=WFCGFloatY(130);
//            lineIV.x=WFCGFloatX(15)+WFCGFloatX(120)*(i%3);
//            lineIV.y=WFCGFloatY(15)+WFCGFloatY(150)*(i/3);
//            [cell.contentView addSubview:lineIV];
//            [lineIV sd_setImageWithURL:[NSURL URLWithString:model.shadeImgFileName] placeholderImage:DefaultImage];
//            lineIV.contentMode=UIViewContentModeScaleAspectFill;
//            lineIV.layer.masksToBounds=YES;
//
//
//            UILabel* titleLab=[UILabel new];
//            titleLab.x=WFCGFloatX(0);
//            titleLab.y=WFCGFloatY(100);
//            titleLab.width=WFCGFloatX(105);
//            titleLab.height=WFCGFloatY(30);
//            titleLab.textColor=[UIColor whiteColor];
//            titleLab.font=SYSTEMFONT(15);
//            [lineIV addSubview:titleLab];
//            titleLab.text=model.name;
//            titleLab.textAlignment=NSTextAlignmentCenter;
//
//            [lineIV addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
//                TKL_HomeCityDetailVC* vc=[[TKL_HomeCityDetailVC alloc] init];
//                vc.passID=[NSString stringWithFormat:@"%ld",model.id];
//                [selfp.navigationController pushViewController:vc animated:YES];
//            }];
//        }
        return cell;
    }
    
    if (indexPath.section==2)
    {
        static NSString* identify=@"ppPhonecell333";
        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell)
        {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor whiteColor];
        }
        [cell.contentView removeAllSubviews];
        
       // BmobObject* mode=[self.dataSource[indexPath.row] objectAtIndex:0];
        UIImageView* lineIV=[UIImageView new];
        lineIV.width=WFCGFloatX(345);
        lineIV.x=WFCGFloatX(15);
        lineIV.y=WFCGFloatY(15);
        lineIV.backgroundColor=Main_Color_BG;
        [cell.contentView addSubview:lineIV];
//        [lineIV sd_setImageWithURL:[NSURL URLWithString:[mode objectForKey:@"image"]] placeholderImage:DefaultImage];
//        lineIV.contentMode=UIViewContentModeScaleAspectFill;
//        lineIV.layer.masksToBounds=YES;
        lineIV.image=[UIImage imageNamed:[self.dataSource[indexPath.row] objectAtIndex:0]];
       // lineIV.width=lineIV.image.size.width;
        lineIV.height=lineIV.width*(lineIV.image.size.height/lineIV.image.size.width);
        
        UILabel* titleLab=[UILabel new];
        titleLab.x=WFCGFloatX(0);
        titleLab.y=lineIV.bottom;
        titleLab.width=SCREEN_WIDTH;
        titleLab.height=WFCGFloatY(40);
        titleLab.textColor=HEXCOLOR(@"#171717");
        titleLab.font=SYSTEMFONT(15);
        [cell.contentView addSubview:titleLab];
        titleLab.text=[NSString stringWithFormat:@"2019年4月第%ld期",indexPath.row+1];
        titleLab.textAlignment=NSTextAlignmentCenter;
        
        return cell;
    }
    
    static NSString* identify=@"ppPhonecell";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor whiteColor];
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return WFCGFloatY(160);
    }
    if (indexPath.section==1)
    {
        float ww=(SCREEN_WIDTH-WFCGFloatX(75))/4;
        return ww+WFCGFloatY(30);
    }
    if (indexPath.section==2)
    {
        UIImage* image=[UIImage imageNamed:[self.dataSource[indexPath.row] objectAtIndex:0]];
        // lineIV.width=lineIV.image.size.width;
       return WFCGFloatX(345)*(image.size.height/image.size.width)+WFCGFloatY(55);
    }
    return WFCGFloatY(200);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=[UIColor whiteColor];
    
    if (section!=0&&section!=1)
    {
        UILabel* titleLab=[UILabel new];
        titleLab.x=WFCGFloatX(0);
        titleLab.y=WFCGFloatY(0);
        titleLab.width=SCREEN_WIDTH;
        titleLab.height=WFCGFloatY(45);
        titleLab.textColor=RGBACOLOR(253, 116, 135, 1);
        titleLab.font=SYSTEMBOLDFONT(17);
        [view addSubview:titleLab];
        titleLab.text=section==1?@"爱心城市":section==2?@"精品推荐":@"推荐摄影师";
        titleLab.textAlignment=NSTextAlignmentCenter;
    }
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section!=0&&section!=1) {
        return WFCGFloatY(45);
    }
    return 0.0001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=Main_Color_BG;
    
    if (section==2)
    {
        view.backgroundColor=[UIColor whiteColor];
        UILabel* titleLab=[UILabel new];
        titleLab.x=WFCGFloatX(150);
        titleLab.y=WFCGFloatY(20);
        titleLab.width=WFCGFloatX(75);
        titleLab.height=WFCGFloatY(30);
        titleLab.textColor=HEXCOLOR(@"#666666");
        titleLab.font=SYSTEMFONT(14);
       // [view addSubview:titleLab];
        titleLab.text=@"查看更多";
        titleLab.textAlignment=NSTextAlignmentCenter;
        [WFFunctions WFUIaddbordertoView:titleLab radius:5 width:0.8 color:HEXCOLOR(@"#999999")];
    }
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==2) {
        return WFCGFloatY(55);
    }
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==2)
    {
        
        TKL_ImageDetailVC* vc=[[TKL_ImageDetailVC alloc] init];
        vc.passObj=self.dataSource[indexPath.row];
        vc.type=2;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{

//    TKL_HomeAdModel* model = self.viewModel.itemArr[index];
//    GCBaseWebVC* vc=[[GCBaseWebVC alloc] init];
//    vc.information=@{@"url":model.content};
//    [self.navigationController pushViewController:vc animated:YES];

}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
